﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.Scheduling;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    /// <summary>
    /// 班别维护
    /// </summary>
    public class SchedulingTypeController : BaseController
    {
        // GET: Scheduling/ScheduingType
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pagesize", 30);
            string illareatype = WebUtils.GetString("illareatype");
            if (illareatype.Equals("0")) {
                illareatype = loginUser.DepartCode;
            }
            string type = WebUtils.GetString("type");
            if (type.Equals("0")) type = string.Empty;
            OperateResult resultInfo = ScheduleManager.GetScheduleTypeList(pageIndex, pageSize, type, illareatype);
            string NoDate = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1)
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                return JsonDate(listData);
            }
            return Content(NoDate);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit()
        {
            SchedulingCategory schedulingCategory = new SchedulingCategory();
            int id = WebUtils.GetInt("id");
            if (id > 0)
            {
                //编辑 取数据赋值
                OperateResult result = ScheduleManager.GetScheduleType(id);
                if (result.Result == 1)
                {
                    schedulingCategory = (SchedulingCategory)result.Body;
                }

                result = ScheduleManager.CheckScheduingType(id);
                if(result.Result==1){
                    ViewBag.IsEdit = (Boolean)result.Body;
                }
            }
            else
            {
                ViewBag.IsEdit = true;
            }
            
            return View(schedulingCategory);
        }

        /// <summary>
        /// 添加/编辑班别
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save()
        {
            SchedulingCategory schedulingCategory = new SchedulingCategory();
            schedulingCategory.CategoryType = WebUtils.GetString("hdnScheduleType");
            schedulingCategory.Name =  WebUtils.GetString("txtName");
            schedulingCategory.Code =  WebUtils.GetString("txtCode");
            schedulingCategory.Color =  WebUtils.GetString("hdnColor");
            schedulingCategory.CreateTime = DateTime.Now;
            schedulingCategory.CreateUserID = LoginManager.GetLoginUser().Id.ToString();
            schedulingCategory.DepartmentCode =  WebUtils.GetString("hdnillArea");
            schedulingCategory.Enabled = DataConvert.StrToInt(WebUtils.GetString("hdnEnabled"));
            schedulingCategory.EndWorkTime = WebUtils.GetString("txtEndWorkTime");
            schedulingCategory.Id = DataConvert.StrToInt(WebUtils.GetString("hdnId"), 0);
            schedulingCategory.Ratio = DataConvert.StrToDecimal(WebUtils.GetString("txtRatio"), 0, 2);
            schedulingCategory.Sort = DataConvert.StrToInt(WebUtils.GetString("txtiSort"), 0);
            schedulingCategory.StatrWorkTime = WebUtils.GetString("txtStartWorkTime");
            schedulingCategory.WorkHours = DataConvert.StrToDecimal(WebUtils.GetString("txtWorkHours"), 0, 1);
            schedulingCategory.IsDel = 0;
            OperateResult resultInfo = ScheduleManager.SaveScheduleType(schedulingCategory);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除班别维护
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id > 0)
            {
                resultInfo = ScheduleManager.GetScheduleType(id);
                if (resultInfo.Result == 1)
                {
                    SchedulingCategory schedulingCategory = (SchedulingCategory)resultInfo.Body;
                    schedulingCategory.IsDel = 1;
                    resultInfo = ScheduleManager.SaveScheduleType(schedulingCategory);
                }
            }
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 班别状态变化
        /// </summary>
        /// <returns></returns>
        public ActionResult Enabled()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id > 0)
            {
                resultInfo = ScheduleManager.GetScheduleType(id);
                if (resultInfo.Result == 1)
                {
                    SchedulingCategory schedulingCategory = (SchedulingCategory)resultInfo.Body;
                    schedulingCategory.Enabled = schedulingCategory.Enabled == 0 ? 1 : 0;
                    resultInfo = ScheduleManager.SaveScheduleType(schedulingCategory);                    
                }
            }
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 班别排序维护
        /// </summary>
        /// <returns></returns>
        public ActionResult Sort()
        {
            return View();
        }

        /// <summary>
        /// 保存班别排序维护
        /// </summary>
        /// <returns></returns>
        public ActionResult SaveSort()
        {
            OperateResult resultInfo = new OperateResult();
            return JsonDate(resultInfo);
        }
    }
}